package com.yunkeji.system.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.domain.dto.PayLogDTO;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.enums.app.InviteRewardType;
import com.yunkeji.common.enums.app.PayLogTypeEnums;
import com.yunkeji.common.msg.PushMessage;
import com.yunkeji.dao.domain.InviteReward;
import com.yunkeji.dao.domain.PhoneLog;
import com.yunkeji.dao.mapper.AppUserMapper;
import com.yunkeji.dao.mapper.InviteRewardMapper;
import com.yunkeji.dao.mapper.PhoneLogMapper;
import com.yunkeji.dao.vo.InviteRewardVo;
import com.yunkeji.dao.vo.PhoneLogVo;
import com.yunkeji.system.service.IInviteRewardService;
import com.yunkeji.system.service.IPhoneLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * 手机号码修改记录业务层处理
 *
 * @author 雷擎宇
 * @date 2024-11-14
 */
@RequiredArgsConstructor
@Service
public class InviteRewardServiceImpl implements IInviteRewardService {

    private final InviteRewardMapper baseMapper;
    private final AppUserMapper userMapper;

    @Override
    public TableDataInfo<InviteRewardVo> queryPageList(InviteReward bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InviteReward> lqw = buildQueryWrapper(bo);
        Page<InviteRewardVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<InviteReward> buildQueryWrapper(InviteReward bo) {
        LambdaQueryWrapper<InviteReward> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, InviteReward::getType, bo.getType());
        lqw.orderByDesc(InviteReward::getCreateTime);
        return lqw;
    }

    @Override
    public Boolean insert(InviteReward log) {
        baseMapper.insert(log);
        return true;
    }

    @Override
    public Boolean edit(InviteReward log) {
        baseMapper.updateById(log);
        return true;
    }


    private BigDecimal inviteRewardHandler(String code, Long traceId, BigDecimal price, Long userId) {
        if(Objects.isNull(userId)){
            return BigDecimal.ZERO;
        }
        InviteReward reward = baseMapper.selectOne(
            Wrappers.lambdaQuery(InviteReward.class)
                .eq(InviteReward::getCode, code));
        if(Objects.isNull(reward)){
            return BigDecimal.ZERO;
        }
        if(reward.getAmount().doubleValue() > 0){
            BigDecimal amount;
            if(reward.getType().equals(0)){
                amount = reward.getAmount();
            } else {
                amount = reward.getAmount().divide(BigDecimal.valueOf(100),2, RoundingMode.DOWN)
                    .multiply(price).setScale(0, RoundingMode.DOWN);
            }
            if (NumberUtil.isGreaterOrEqual(amount, BigDecimal.ONE)) {
                userMapper.logAddDiamondBalance(
                    PayLogDTO.builder()
                        .traceId(traceId)
                        .uid(userId)
                        .changeNum(amount)
                        .serviceTag(reward.getDescription() + ":" + traceId)
                        .logType(PayLogTypeEnums.收益_邀请充值收益)
                        .build());
                return amount;
            }
        }
        return BigDecimal.ZERO;
    }

    @Override
    public void inviteRewardFirst(Long traceId, BigDecimal price, Long userId) {
        BigDecimal amount = inviteRewardHandler(InviteRewardType.被邀请人首充.getCode(), traceId, price, userId);
        if(amount.doubleValue() > 0) {
            PushMessage.sendSysNotify(
                userMapper.selectById(userId).getPlatformId(),
                "您邀请的用户已首次充值成功，您获得了" + amount + "钻石奖励，可前往我的钱包查看！");
        }
    }

    @Override
    public void inviteRewardRecharge(Long traceId, BigDecimal price, Long userId) {
        BigDecimal amount = inviteRewardHandler(InviteRewardType.被邀请人充值.getCode(), traceId, price, userId);
        if(amount.doubleValue() > 0) {
            PushMessage.sendSysNotify(
                userMapper.selectById(userId).getPlatformId(),
                "您邀请的用户已成功充值，您获得了" + amount + "钻石奖励，可前往我的钱包查看！");
        }
    }

}
